{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Material Library\n",
    "================\n",
    "\n",
    "PyNE comes with a pre-built library of materials  Most of this data comes from [a materials compendium by PNNL](http://www.pnnl.gov/main/publications/external/technical_reports/PNNL-15870Rev1.pdf), which is gives canonical values for normal materials.  This notebook demonstrates how to load and use this data via the `MaterialLibrary` class.  First the imports!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the path to the nuc_data.h5 database\n",
    "from pyne import nuc_data\n",
    "\n",
    "# the material library class itself\n",
    "from pyne.material_library import MaterialLibrary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `MaterialLibrary` class is a dict-like class which maps string names to `Material` objects.  We can instantiate this class directly from the database as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "mats = MaterialLibrary(nuc_data, datapath='/material_library/materials')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also take a gander at the keys in this dictionary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Au',\n",
       " 'Ar',\n",
       " 'As',\n",
       " 'Zirconium Hydride (Zr5H8)',\n",
       " 'Bone, Compact (ICRU)',\n",
       " 'Plutonium Nitrate',\n",
       " 'Boral (Aluminum 5% boron alloy)',\n",
       " 'Earth, U.S. Average',\n",
       " 'Nylon, Type 11 (Rilsan)',\n",
       " 'Boron']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# employing a decode from byte literals to unicode str values\n",
    "# otherwise just use: list(mats.keys())[:10]\n",
    "[mat.decode('utf-8') for mat in list(mats.keys())[:10]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the values too!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pyne.material.Material({60120000: 0.006738245111766667, 60130000: 7.897265541777496e-05, 140280000: 0.0060307851831815865, 140290000: 0.0003173145988016552, 140300000: 0.00021662843826844154, 150310000: 0.0002625891288100673, 160320000: 0.00019131705365087835, 160330000: 1.557774006043863e-06, 160340000: 9.094152481841308e-06, 160360000: 2.2657407442146268e-08, 240500000: 0.007165923113837514, 240520000: 0.14370585573699474, 240530000: 0.016608861078294093, 240540000: 0.004212251985148446, 250550000: 0.006564728220251683, 260540000: 0.04533198404164438, 260560000: 0.7379391848627866, 260570000: 0.017347052631454484, 260580000: 0.0023490356196681664, 420920000: 0.0006858785703804202, 420940000: 0.00044131037576579846, 420950000: 0.0007721150960341245, 420960000: 0.0008211250804812777, 420970000: 0.0004778107505804997, 420980000: 0.0012264572169225815, 421000000: 0.0005038988659627156}, 1.0, 7.8, -1.0, {\"name\":\"Steel, Stainless 440\"})"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mats['Steel, Stainless 440']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can do everything you normaly would with these materials, like print them out in MCNP form!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C name: Steel, Stainless 440\n",
      "C density = 7.80000\n",
      "m?\n",
      "     6012 -5.2558e-02\n",
      "     6013 -6.1599e-04\n",
      "     14028 -4.7040e-02\n",
      "     14029 -2.4751e-03\n",
      "     14030 -1.6897e-03\n",
      "     15031 -2.0482e-03\n",
      "     16032 -1.4923e-03\n",
      "     16033 -1.2151e-05\n",
      "     16034 -7.0934e-05\n",
      "     16036 -1.7673e-07\n",
      "     24050 -5.5894e-02\n",
      "     24052 -1.1209e+00\n",
      "     24053 -1.2955e-01\n",
      "     24054 -3.2856e-02\n",
      "     25055 -5.1205e-02\n",
      "     26054 -3.5359e-01\n",
      "     26056 -5.7559e+00\n",
      "     26057 -1.3531e-01\n",
      "     26058 -1.8322e-02\n",
      "     42092 -5.3499e-03\n",
      "     42094 -3.4422e-03\n",
      "     42095 -6.0225e-03\n",
      "     42096 -6.4048e-03\n",
      "     42097 -3.7269e-03\n",
      "     42098 -9.5664e-03\n",
      "     42100 -3.9304e-03\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(mats['Steel, Stainless 440'].mcnp())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
